package commonClass.socket.stickPackage;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ThreadSocketServer {
	/** 服务器端口 **/
	public static final int PORT = 8189;

	public static void main(String[] args) {

		try {
			ServerSocket s = new ServerSocket(PORT);
			System.out.println("Listening on port: " + PORT);
			while (true) {
				Socket incoming = s.accept();
				Runnable r = new ThreadedEchoHandler(incoming);
				Thread t = new Thread(r);
				t.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

class ThreadedEchoHandler implements Runnable {

	private Socket incoming;
	/** 回车和换行 **/
	public static final byte CR = (byte) '\r';
	public static final byte LF = (byte) '\n';
	
	public ThreadedEchoHandler(Socket i) {
		incoming = i;
	}

	@Override
	public void run() {

		try {
			try {
				
				DataOutputStream dOut = new DataOutputStream(
						incoming.getOutputStream());
				dOut.writeByte(LF);
				dOut.writeByte(CR);
				dOut.write(0);

				dOut.write('s');
				dOut.write('o');
				dOut.write('s');
				dOut.write(0);
				dOut.write(0);
				dOut.write(0);
				dOut.write(2);

				dOut.writeUTF("ksfzhaohui");
				dOut.writeUTF("111111");
				dOut.flush();
				
			} finally {
				incoming.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
